/**
 * created by 江心才子, 2019/8/5
 * Copyright (c) 2019, 270628297@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.wyl.base.jiaozivideo

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import android.widget.SeekBar
import cn.jzvd.JzvdStd
import com.wyl.base.R
import com.wyl.libbase.extend.logD
import com.wyl.libbase.utils.KLog


/**
 * 项目名称：ShortVideo
 * 创建人：江心才子
 * 创建时间：2019-08-05 15:39
 * 内容描述：
 * 修改说明：
 */
class MyJzvdStd : JzvdStd {
    constructor(context: Context) : super(context)

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs)

    override fun init(context: Context) {
        super.init(context)
    }

    override fun getLayoutId(): Int {
        return super.getLayoutId()
    }

    override fun startVideo() {
        super.startVideo()
        logD("startVideo")

    }

    var time = -1L

    fun startVideo(time: Long) {
        this.time = time
        startVideo()
    }

    override fun onStopTrackingTouch(seekBar: SeekBar) {
        super.onStopTrackingTouch(seekBar)
        logD("Seek position ")
    }

    override fun gotoScreenFullscreen() {
        super.gotoScreenFullscreen()
        logD("goto Fullscreen")
    }

    override fun gotoScreenNormal() {
        super.gotoScreenNormal()
        logD("quit Fullscreen")
    }

    override fun autoFullscreen(x: Float) {
        super.autoFullscreen(x)
        logD("auto Fullscreen")
    }

    override fun onClickUiToggle() {
        super.onClickUiToggle()
        logD("click blank")
    }


    /* *************** 1. 了解播放器的状态 ******************/
    /*
    继承JzvdStd之后，可以通过父类的state，取得当前的播放状态。

    STATE_IDLE = -1;                    未知状态，指控件被new出来之后什么都没做
    STATE_NORMAL = 0;                   普通状态
    STATE_PREPARING = 1;                视频准备状态
    STATE_PREPARING_CHANGING_URL = 2;   播放中切换url的准备状态
    STATE_PREPARED = 3;
    STATE_PLAYING = 4;                  播放中状态
    STATE_PAUSE = 5;                    暂停状态
    STATE_AUTO_COMPLETE = 6;            自动播放完成状态
    STATE_ERROR = 7;                    错误状态
     */
    /** 进入普通状态，通常指setUp之后 */
    override fun onStateNormal() {
        super.onStateNormal()
        KLog.d()
    }

    /** 进入准备中状态，就是loading状态 */
    override fun onStatePreparing() {
        super.onStatePreparing()
        KLog.d()
    }

    /** 进入播放状态 */
    override fun onStatePlaying() {
        super.onStatePlaying()
        KLog.d()
    }

    override fun onStatePause() {
        super.onStatePause()
        KLog.d()
    }

    override fun onStateError() {
        super.onStateError()
        KLog.d()
    }

    override fun onStateAutoComplete() {
        KLog.d()
        super.onStateAutoComplete()
        completeListener.invoke()
    }

    private var completeListener = {}

    fun setOnCompleteListener(listener: () -> Unit) {
        completeListener = listener
    }

    /* *************** 2. 了解当前屏幕类型 ******************/
    /*
    在继承JzvdStd后，通过screen变量，取得当前屏幕类型
    SCREEN_NORMAL = 0;
    SCREEN_FULLSCREEN = 1;
    SCREEN_TINY = 2;
     */


    /* *************** 3. 事件 ******************/
    /** 取得每次播放器设置底部seekBar的进度回调 */
    override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
        super.onProgressChanged(seekBar, progress, fromUser)
    }

    override fun onProgress(progress: Int, position: Long, duration: Long) {
        super.onProgress(progress, position, duration)
    }

    override fun changeUrl(url: String?, title: String?, seekToInAdvance: Long) {
        super.changeUrl(url, title, seekToInAdvance)
    }

    override fun onClick(v: View) {
        super.onClick(v)
        val i = v.id
        if (i == cn.jzvd.R.id.fullscreen) {
            logD("onClick: fullscreen button")
        } else if (i == R.id.start) {
            logD("onClick: start button")
        }
    }

    override fun onTouch(v: View, event: MotionEvent): Boolean {
        super.onTouch(v, event)
        val id = v.id
        if (id == cn.jzvd.R.id.surface_container) {
            when (event.action) {
                MotionEvent.ACTION_UP -> {
                    if (mChangePosition) {
                        logD("Touch screen seek position")
                    }
                    if (mChangeVolume) {
                        logD("Touch screen change volume")
                    }
                }
            }
        }
        return false
    }


    /* *******************************************/
    //changeUiTo 真能能修改ui的方法
    override fun changeUiToNormal() {
        super.changeUiToNormal()
    }

    override fun changeUiToPreparing() {
        super.changeUiToPreparing()
    }

    override fun changeUiToPlayingShow() {
        super.changeUiToPlayingShow()
    }

    override fun changeUiToPlayingClear() {
        super.changeUiToPlayingClear()
    }

    override fun changeUiToPauseShow() {
        super.changeUiToPauseShow()
    }

    override fun changeUiToPauseClear() {
        super.changeUiToPauseClear()
    }

    override fun changeUiToComplete() {
        super.changeUiToComplete()
    }

    override fun changeUiToError() {
        super.changeUiToError()
    }

    override fun onInfo(what: Int, extra: Int) {
        super.onInfo(what, extra)
    }

    override fun onError(what: Int, extra: Int) {
        super.onError(what, extra)
    }

}